package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;

    @GetMapping("/list.html")
    public String list(SearchParam searchParam, Model model){
        //回显页面 关键词
        model.addAttribute("searchParam",searchParam);
        //根据关键词条件查询出的返回值对象
        SearchResponseVo responseVo = listFeignClient.list(searchParam);
        //回显品牌列表
        model.addAttribute("trademarkList",responseVo.getTrademarkList());
        //URLParam ===整一个 创建urlParam方法
        String urlParam = buildUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);
        //平台属性
        model.addAttribute("attrsList",responseVo.getAttrsList());

        //8: 排序回显  ===整一个 构建页面排序规则的方法
        Map orderMap = buildOrderMap(searchParam);
        model.addAttribute("orderMap",orderMap);
        //结果集
        model.addAttribute("goodsList",responseVo.getGoodsList());
        //当前页 与 总页数
        model.addAttribute("pageNo",responseVo.getPageNo());
        model.addAttribute("totalPages",responseVo.getTotalPages());

        //9: 回显品牌
        String trademarkParam = buildTrademarkParam(searchParam);
        model.addAttribute("trademarkParam",trademarkParam);
        //10: 回显平台属性   平台属性名称:平台属性值
        List<Map> propsParamList = buildPropsParamList(searchParam);
        model.addAttribute("propsParamList",propsParamList);

        return "list/index";

    }

    //构建页面排序  红底样式 ASC DESC回显
    private Map buildOrderMap(SearchParam searchParam) {
        Map orderMap = new HashMap();
        String order = searchParam.getOrder();
        //order = 1:asc  排序规则 0:asc
        if (!StringUtils.isEmpty(order)){
            String[] o = order.split(":");
            orderMap.put("type",o[0]);
            orderMap.put("sort",o[1]);
        }else {
            orderMap.put("type",1);
            orderMap.put("sort","desc");
        }
        return orderMap;
    }

    //回显平台属性   平台属性名称:平台属性值
    private List<Map> buildPropsParamList(SearchParam searchParam) {

        String[] props = searchParam.getProps();
        if(null != props && props.length > 0){
            //props=23:4G:运行内存
            return Arrays.stream(props).map(prop -> {
                String[] p = prop.split(":");
                Map map = new HashMap();
                map.put("attrId",p[0]);
                map.put("attrValue",p[1]);
                map.put("attrName",p[2]);
                return map;
            }).collect(Collectors.toList());
        }
        return null;
    }

    //构建品牌回显
    private String buildTrademarkParam(SearchParam searchParam) {

        //trademark=2:华为
        String trademark = searchParam.getTrademark();
        if(!StringUtils.isEmpty(trademark)){
            String[] t = trademark.split(":");
            return "品牌:" + t[1];
        }
        return null;
    }

    //构建URLParam    springIOC容器 单例模式 本就线程安全
    private String buildUrlParam(SearchParam searchParam) {
        //StringBuilder 性能高 线程不安全   上班建议此方式
        //StringBuffer  性能低 线程安全
        StringBuilder sb = new StringBuilder();
        sb.append("http://list.gmall.com/list.html?");
        //keyword无需验空
        String keyword = searchParam.getKeyword();
        //关键词
        if(!StringUtils.isEmpty(keyword)){
            System.out.println("keyword:" + keyword);
            sb.append("keyword=").append(keyword);
        }
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)){
            sb.append("&trademark=").append(trademark);
        }
        String[] props = searchParam.getProps();
        if (null != props && props.length > 0){
            for (String prop : props) {
                sb.append("&props=").append(prop);
            }
        }
        //String url = "/list?keyword="+ searchParam.getKeyword()+"";
        return sb.toString();
    }


}
